package com.yy.wga.mp.module.goods.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yy.wga.mp.module.goods.service.GoodsHeadSupplierInfoService;
import com.yy.wga.mp.module.goods.service.GoodsHeadSupplierInfoService;
import com.yy.wga.mp.common.interceptor.RequestUserHolder;
import com.yy.wga.mp.common.utils.common.SpringUtils;
import com.yy.wga.mp.common.utils.common.StringUtils;
import com.yy.wga.mp.common.utils.mapping.BeansUtils;
import com.yy.wga.mp.module.goods.api.response.product.GetHeadSupplierProductDetailDTO;
import com.yy.wga.mp.module.goods.api.response.product.PromoteProductDetailDTO;
import com.yy.wga.mp.module.goods.entity.bo.GoodsIdBO;
import com.yy.wga.mp.module.goods.entity.pojo.GoodsHeadSupplierInfoDO;
import com.yy.wga.mp.module.goods.entity.pojo.GoodsSpuDO;
import com.yy.wga.mp.module.goods.manager.ProductManager;
import com.yy.wga.mp.module.goods.mapper.GoodsHeadSupplierInfoMapper;
import com.yy.wga.mp.module.goods.service.GoodsHeadSupplierInfoService;
import com.yy.wga.mp.module.goods.service.GoodsHeadSupplierInfoService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.*;

@Service
public class GoodsHeadSupplierInfoServiceImpl extends ServiceImpl<GoodsHeadSupplierInfoMapper, GoodsHeadSupplierInfoDO> implements GoodsHeadSupplierInfoService {

    @Resource
    private GoodsHeadSupplierInfoMapper goodsHeadSupplierInfoMapper;


    @Override
    public boolean syncInfoToDb(Integer planType,GoodsIdBO goodsIdBO) {
        // 详情
        PromoteProductDetailDTO.Product detail = SpringUtils.getBean(ProductManager.class).getPromoteProductDetail(planType, goodsIdBO);
        if (Objects.isNull(detail)) {
            return false;
        }

        GoodsHeadSupplierInfoDO goodsHeadSupplierInfoDO = this.toGoodsHeadSupplierInfoDO(detail);
        GoodsHeadSupplierInfoDO goodsHeadSupplierInfoByDb = this.getGoodsHeadSupplierInfo(goodsIdBO);
        if (Objects.nonNull(goodsHeadSupplierInfoByDb)) {
            BeansUtils.copy(goodsHeadSupplierInfoDO, goodsHeadSupplierInfoByDb);
            this.updateById(goodsHeadSupplierInfoDO);
        } else {
            goodsHeadSupplierInfoDO.setAppKey(RequestUserHolder.getAppKey());
            this.save(goodsHeadSupplierInfoDO);
        }
        return true;

    }

    @Override
    public GoodsHeadSupplierInfoDO getGoodsHeadSupplierInfo(GoodsIdBO goodsIdBO) {
        return this.lambdaQuery().eq(GoodsHeadSupplierInfoDO::getShopAppid, goodsIdBO.getShopAppid()).eq(GoodsHeadSupplierInfoDO::getSpuId, goodsIdBO.getProductId()).eq(GoodsHeadSupplierInfoDO::getAppKey, RequestUserHolder.getAppKey()).eq(GoodsHeadSupplierInfoDO::getDeleted, 0).last(" LIMIT 1").one();
    }

    @Override
    public List<GoodsHeadSupplierInfoDO> getGoodsHeadSupplierInfos(List<GoodsIdBO> goodsIdBOS) {
        if (CollectionUtils.isEmpty(goodsIdBOS)){
            return Collections.emptyList();
        }
        return baseMapper.getGoodsHeadSupplierInfos(RequestUserHolder.getAppKey(), goodsIdBOS);
    }

    @Override
    public void sync(List<GoodsIdBO> syncGoodsIdBOList) {
        goodsHeadSupplierInfoMapper.removeNotExist(RequestUserHolder.getAppKey(), syncGoodsIdBOList);
    }

    private GoodsHeadSupplierInfoDO toGoodsHeadSupplierInfoDO(PromoteProductDetailDTO.Product detail) {
        GoodsHeadSupplierInfoDO goodsSpu = new GoodsHeadSupplierInfoDO();
        goodsSpu.setShopAppid(detail.getShopAppid());
        goodsSpu.setSpuId(detail.getProductId());
        PromoteProductDetailDTO.ProductInfo productInfo = detail.getProductInfo();
        PromoteProductDetailDTO.CommissionInfo commissionInfo = detail.getCommissionInfo();

        List<PromoteProductDetailDTO.Sku> skuResps = productInfo.getSkus();
        Integer salePrice = skuResps.get(0).getSalePrice();
        List<GoodsSpuDO.Sku> skuInfos = new ArrayList<>();
        for (PromoteProductDetailDTO.Sku skuResp : skuResps) {
            List<GoodsSpuDO.Attr> goodsAttrs = new ArrayList<>();
            for (PromoteProductDetailDTO.Attr attr : skuResp.getSkuAttrs()) {
                String attrValue = attr.getAttrValue();
                if (StringUtils.isNotBlank(attrValue)) {
                    GoodsSpuDO.Attr goodsAttr = new GoodsSpuDO.Attr();
                    goodsAttr.setAttrKey(attr.getAttrKey());
                    goodsAttr.setAttrValue(Arrays.asList(attrValue.split(";")));
                    goodsAttrs.add(goodsAttr);
                }
            }
            GoodsSpuDO.Sku sku = BeansUtils.map(skuResp, GoodsSpuDO.Sku.class);
            sku.setSkuAttrs(goodsAttrs);
            skuInfos.add(sku);
        }

        goodsSpu.setSkus(skuInfos);
        goodsSpu.setSpuId(detail.getProductId());
        goodsSpu.setTitle(productInfo.getTitle());
        goodsSpu.setHeadImg(productInfo.getHeadImgs());
        goodsSpu.setSalePrice(salePrice);
        goodsSpu.setRatio(commissionInfo.getRatio());
        goodsSpu.setServiceRatio(commissionInfo.getServiceRatio());
        List<PromoteProductDetailDTO.Cat> catsV2 = productInfo.getCatsV2();
        if (!CollectionUtils.isEmpty(catsV2)) {
            int index = 1;
            for (PromoteProductDetailDTO.Cat cat : catsV2) {
                String catId = cat.getCatId();
                switch (index) {
                    case 1:
                        goodsSpu.setFirstCateId(catId);
                        break;
                    case 2:
                        goodsSpu.setSecondCateId(catId);
                        break;
                    case 3:
                        goodsSpu.setThirdCateId(catId);
                        break;
                }
                index++;
            }
        }
        return goodsSpu;
    }
}
